﻿using System;
using System.Collections.Generic;
using System.Linq;
using Celeriq.Common;
using System.Web.UI.WebControls;
using PJB.FacetedNavigation;

namespace PJBWebsite.UserControls
{
    #region DimensionDisplayEventArgs

    public class DimensionDisplayEventArgs : EventArgs
    {
        #region Class Members

        private readonly ListingQuery _query = null;
        private readonly RefinementItem _refinement = null;

        #endregion

        #region Constructors

        public DimensionDisplayEventArgs(ListingQuery query, RefinementItem refinement)
        {
            _query = query;
            _refinement = refinement;
        }

        #endregion

        #region Property Implmentations

        public ListingQuery Query
        {
            get { return _query; }
        }

        public RefinementItem Refinement
        {
            get { return _refinement; }
        }

        #endregion

    }

    #endregion

    public abstract class DimensionDisplayControl : System.Web.UI.UserControl
    {
        #region Class Members

        private int _itemsPerRow = 3;
        private int _maxItems = 0;
        private string _moreLinkText = string.Empty;
        private string _moreLinkURL = string.Empty;
        private string _suffix = string.Empty;
        private bool _allowCount = true;
        private bool _allowWidth = true;
        private bool _alignColumns = true;
        private ListingResults _results = null;

        #endregion

        #region Events

        public event EventHandler<DimensionDisplayEventArgs> LinkSetup;

        protected void OnLinkSetup(DimensionDisplayEventArgs e)
        {
            if (this.LinkSetup != null)
                this.LinkSetup(this, e);
        }

        #endregion

        #region Property Implementations

        public int ItemsPerRow
        {
            get { return _itemsPerRow; }
            set { _itemsPerRow = value; }
        }

        public int MaxItems
        {
            get { return _maxItems; }
            set { _maxItems = value; }
        }

        public string MoreLinkText
        {
            get { return _moreLinkText; }
            set { _moreLinkText = value; }
        }

        public string MoreLinkURL
        {
            get { return _moreLinkURL; }
            set { _moreLinkURL = value; }
        }

        public string Suffix
        {
            get { return _suffix; }
            set { _suffix = value; }
        }

        public bool AllowCount
        {
            get { return _allowCount; }
            set { _allowCount = value; }
        }

        public bool AllowWidth
        {
            get { return _allowWidth; }
            set { _allowWidth = value; }
        }

        public bool AlignColumns
        {
            get { return _alignColumns; }
            set { _alignColumns = value; }
        }

        public ListingResults QueryResults
        {
            get { return _results; }
        }

        #endregion

        #region Methods

        //public void Populate()
        //{
        //    this.Populate(false);
        //}

        //public abstract void Populate(bool isLimited);
        protected abstract PlaceHolder MorePanel { get; }
        protected abstract Table MainTable { get; }

        protected virtual void Populate(IEnumerable<RefinementItem> refinementList, bool isLimited)
        {
            //if (dItem == null)
            //{
            //  this.Visible = false;
            //  return;
            //}

            try
            {
                var index = 0;

                //Round down to even multiple of ItemsPerRow
                var maxItems = this.MaxItems;
                if (maxItems > 0) maxItems = maxItems - (maxItems % ItemsPerRow);

                if (refinementList.Count() == 0)
                {
                    this.Visible = false;
                    return;
                }

                foreach (var rItem in refinementList)
                {
                    //if (rItem.PrimaryKey != null)
                    //{
                    TableRow row = null;
                    if (index % this.ItemsPerRow == 0)
                    {
                        if ((index == maxItems) && (maxItems > 0))
                        {
                            //Add the more link if necessary
                            if (this.MoreLinkText != string.Empty && this.MoreLinkURL != string.Empty)
                            {
                                var l = new Literal
                                {
                                    Text =
                                        "<div align=\"right\"><a href=\"" + this.MoreLinkURL + "\" title=\"" + this.MoreLinkText + "\">" +
                                        this.MoreLinkText + "</a></div>"
                                };
                                this.MorePanel.Controls.Add(l);
                            }
                            return;
                        }
                        row = new TableRow();
                        if (!this.AlignColumns)
                        {
                            //If we are NOT aligning columns then there will be one column in each row so create it
                            row.Cells.Add(new TableCell());
                        }

                        this.MainTable.Rows.Add(row);
                    }
                    else
                    {
                        row = this.MainTable.Rows[this.MainTable.Rows.Count - 1];
                    }

                    var query2 = new ListingQuery();
                    this.OnLinkSetup(new DimensionDisplayEventArgs(query2, rItem));

                    //Create the link
                    var link = new Literal();
                    var linkText = rItem.FieldValue;
                    if (this.Suffix != string.Empty) linkText += " " + this.Suffix;
                    if (this.AllowCount) linkText += " (" + rItem.Count + ")";
                    link.Text = "<a href=\"" + query2.ToString() + "\" title=\"" + linkText + "\">" + linkText + "</a>";

                    if (this.AlignColumns)
                    {
                        //This is all inside of a table with multiple columns
                        var cell = new TableCell();
                        if (this.AllowWidth)
                        {
                            this.MainTable.Width = new Unit(100, UnitType.Percentage);
                            if (index < ItemsPerRow) cell.Width = new Unit((int)(100 / ItemsPerRow), UnitType.Percentage);
                        }
                        row.Cells.Add(cell);
                        cell.Controls.Add(link);
                    }
                    else
                    {
                        //The table has one column per row and 
                        var cell = row.Cells[0];
                        if (this.AllowWidth)
                        {
                            this.MainTable.Width = new Unit(100, UnitType.Percentage);
                            if (index < ItemsPerRow) cell.Width = new Unit((int)(100 / ItemsPerRow), UnitType.Percentage);
                        }
                        cell.Controls.Add(link);
                    }

                    index++;
                }
                //}

            }
            catch (Exception ex)
            {
                throw;
            }
        }

        #endregion

    }
}
